﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERP.Manager.Core;
using ERP.Manager.DataEntity;
using ERP.Manager.DataProvider;
using ERP.Manager.DataHelper;
using System.Data.SqlClient;
using System.Data;
namespace ERP.Manager.SQLServerProvider
{
    public class ECS_categoryinfoRepository : IECS_categoryinfoRepository
    {
        #region 分页查询商品分类信息的方法
        public List<ECS_categoryinfo> all_ecs_categoryinfo_page(int pagecount)
        {
            List<ECS_categoryinfo> list = new List<ECS_categoryinfo>();
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_ECS_CATEGORYINFO_PAGE, new SqlParameter("@pagecount", pagecount));
            try
            {
                while (read.Read())
                {
                    ECS_categoryinfo category = new ECS_categoryinfo();
                    category.cat_id = int.Parse(read["cat_id"].ToString());
                    category.cat_name = read["cat_name"].ToString();
                    category.keywords = read["keywords"].ToString();
                    category.cat_desc = read["cat_desc"].ToString();
                    category.parent_id = int.Parse(read["parent_id"].ToString());
                    category.sort_order = int.Parse(read["sort_order"].ToString());
                    category.template_file = read["template_file"].ToString();
                    category.measure_unit = read["measure_unit"].ToString();
                    category.show_in_nav = int.Parse(read["show_in_nav"].ToString());
                    category.style = read["style"].ToString();
                    category.is_show = int.Parse(read["is_show"].ToString());
                    category.grade = int.Parse(read["grade"].ToString());
                    category.filter_attr = read["filter_attr"].ToString();
                    category.cat_goodscount = all_ecs_category_cat_id_goods_count(category.cat_id);
                    list.Add(category);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion

        #region 查询所有商品分类信息
        public List<ECS_categoryinfo> all_ecs_categroyinfo()
        {
            List<ECS_categoryinfo> list = new List<ECS_categoryinfo>();
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_ECS_CATEGORYINFO);
            try
            {
                while (read.Read())
                {
                    ECS_categoryinfo category = new ECS_categoryinfo();
                    category.cat_id = int.Parse(read["cat_id"].ToString());
                    category.cat_name = read["cat_name"].ToString();
                    category.keywords = read["keywords"].ToString();
                    category.cat_desc = read["cat_desc"].ToString();
                    category.parent_id = int.Parse(read["parent_id"].ToString());
                    category.sort_order = int.Parse(read["sort_order"].ToString());
                    category.template_file = read["template_file"].ToString();
                    category.measure_unit = read["measure_unit"].ToString();
                    category.show_in_nav = int.Parse(read["show_in_nav"].ToString());
                    category.style = read["style"].ToString();
                    category.is_show = int.Parse(read["is_show"].ToString());
                    category.grade = int.Parse(read["grade"].ToString());
                    category.filter_attr = read["filter_attr"].ToString();
                    list.Add(category);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion

        #region 添加商品分类信息的方法
        public int add_ecs_categoryinfo(ECS_categoryinfo category)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.ADD_ECS_CATEGORYINFO,
                new SqlParameter("@cat_name", category.cat_name),
                new SqlParameter("@keywords", category.keywords),
                new SqlParameter("@cat_desc", category.cat_desc),
                new SqlParameter("@parent_id", category.parent_id),
                new SqlParameter("@sort_order", category.sort_order),
                new SqlParameter("@template_file", category.template_file),
                new SqlParameter("@measure_unit", category.measure_unit),
                new SqlParameter("@show_in_nav", category.show_in_nav),
                new SqlParameter("@style", category.style),
                new SqlParameter("@is_show", category.is_show),
                new SqlParameter("@grade", category.grade),
                new SqlParameter("@filter_attr", category.filter_attr));
            return rowcount;
        }
        #endregion

        #region 修改商品分类信息的方法
        public int update_ecs_categoryinfo(ECS_categoryinfo category)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.UPDATE_ECS_CATEGORYINFO,
                new SqlParameter("@cat_name", category.cat_name),
                new SqlParameter("@keywords", category.keywords),
                new SqlParameter("@cat_desc", category.cat_desc),
                new SqlParameter("@measure_unit", category.measure_unit),
                new SqlParameter("@show_in_nav", category.show_in_nav),
                new SqlParameter("@style", category.style),
                new SqlParameter("@is_show", category.is_show),
                new SqlParameter("@grade", category.grade),
                new SqlParameter("@filter_attr", category.filter_attr),
                new SqlParameter("@cat_id", category.cat_id));
            return rowcount;
        }
        #endregion

        #region 删除商品分类信息的方法
        public int delete_ecs_categoryinfo(int cat_id)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.DELETE_ECS_CATEGORYINFO, new SqlParameter("@cat_id", cat_id));
            return rowcount;
        }
        #endregion

        #region 转移商品的方法
        public int update_ecs_categoryinfo_zhuanyishangping(int new_cat_id, int cat_id)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.UPDATE_ECS_CATEGORYINFO_ZHUANYISHANGPING,
                new SqlParameter("@new_cat_id", new_cat_id),
                new SqlParameter("@cat_id", cat_id));
            return rowcount;
        }
        #endregion

        #region 根据商品分类编号查询商品数量
        public int all_ecs_category_cat_id_goods_count(int cat_id)
        {
            var rowcount = SQLHelper.ExecuteScalar(CommandType.Text, ERPSQL.ALL_ECS_CATEGORY_CAT_ID_GOODS_COUNT, new SqlParameter("@cat_id", cat_id));
            return int.Parse(rowcount.ToString());
        }
        #endregion


        #region 根据商品分类编号修改商品导航栏的状态的方法
        public int update_ecs_category_show_in_nav(int cat_id, int show_in_nav)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.UPDATE_ECS_CATEGORY_SHOW_IN_NAV,
                new SqlParameter("@show_in_nav", show_in_nav),
                new SqlParameter("@cat_id", cat_id));
            return rowcount;
        }
        #endregion

        #region 根据商品分类编号修改商品是否显示的状态的方法
        public int update_ecs_category_is_show(int cat_id, int is_show)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.UPDATE_ECS_CATEGORY_IS_SHOW,
                new SqlParameter("@is_show", is_show),
                new SqlParameter("@cat_id", cat_id));
            return rowcount;
        }
        #endregion
    }
}
